package com.ymatou.iapi.thirdpartysync.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.fenqile.api.pop.parameter.Product;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.thirdpartysync.parameter.addProductBean;
import com.ymatou.iapi.thirdpartysync.service.addProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.FQLOpenCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.database.model.thirdpartysync.fenqilebrand;
import com.ymttest.database.model.thirdpartysync.fenqilebrandExample;
import com.ymttest.database.model.thirdpartysync.fenqilecategory;
import com.ymttest.database.model.thirdpartysync.fenqilecategoryExample;
import com.ymttest.database.model.thirdpartysync.fenqileproduct;
import com.ymttest.database.model.thirdpartysync.fenqileproductExample;
import com.ymttest.database.model.thirdpartysync.fenqilesku;
import com.ymttest.database.model.thirdpartysync.fenqileskuExample;
import com.ymttest.database.mongo.jproduct.ProductsWapper;
import com.ymttest.database.sqlwapper.thirdpartysync.thirdpartysyncWapper;
import com.ymttest.utils.EnvSetup;
/**
*添加商品进分期乐
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_addProduct{
	private static addProductBean bean;
	private static addProductCall call;
	private static ProductsWapper mongoDB_productsWapper=new ProductsWapper();
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	
	fenqileproduct product=new fenqileproduct();
	fenqileproductExample productExample=new fenqileproductExample();
	thirdpartysyncWapper syncWapper=new thirdpartysyncWapper(); 
	
	fenqilesku sku=new fenqilesku();
	fenqileskuExample skuExample=new fenqileskuExample();
	
	fenqilecategory category=new fenqilecategory();
	fenqilecategoryExample categoryExample=new fenqilecategoryExample();
	
	fenqilebrand brand=new fenqilebrand();
	fenqilebrandExample brandExample=new fenqilebrandExample();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加商品进分期乐");
	}

	@Before
	public void caseUp() {
		bean=new addProductBean();
		call=new addProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	//校验
	private void check_verify(String productId,AddSellerProductRequest addpro ) throws Exception {
		//查询数据库fenqileproduct
		productExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtSaleStatusEqualTo(1).andYmtProductIdEqualTo(productId);
		List<fenqileproduct> prodcuts_DB=syncWapper.selectByExample(productExample);
		//查询数据库fenqilesku
		skuExample.createCriteria().andActionEqualTo(0).andSyncStatusEqualTo(1).andYmtProductIdEqualTo(productId);
		List<fenqilesku> skus_DB=syncWapper.selectByExample(skuExample);
		//查询数据库fenqilecategory
		categoryExample.createCriteria().andActionEqualTo(0).andYmtCategory3IdEqualTo(addpro.getProduct().getThirdCategoryId());
		List<fenqilecategory> categorys_DB=syncWapper.selectByExample(categoryExample);
		//查询数据库fenqilebrand
		brandExample.createCriteria().andActionEqualTo(0).andYmtBrandIdEqualTo(addpro.getProduct().getBrandId());
		List<fenqilebrand>  brands_DB=syncWapper.selectByExample(brandExample);
		int sTimes=0;
		do{
		 	 if(prodcuts_DB.size()==0 )
			 {
		 		 sTimes+=2000;
				 Thread.sleep(sTimes);
				 prodcuts_DB=syncWapper.selectByExample(productExample);
				 skus_DB=syncWapper.selectByExample(skuExample);
				 categorys_DB=syncWapper.selectByExample(categoryExample);
				 brands_DB=syncWapper.selectByExample(brandExample);
				 Logger.comment("查询数据库休眠的时间："+sTimes);
				 if(prodcuts_DB.size()==0) continue;
			 }else	 break;
	    }while(sTimes<=10000);

		Product searchFenqileSync = null;
		if(sTimes>10000)
			Logger.comment("商品同步到分期乐异常");
		else {
			  searchFenqileSync = ThirdpartysyncCallService.searchFenqileSync(prodcuts_DB.get(0).getFqlProductId());
		}
		Logger.comment("prodcuts_DB:"+prodcuts_DB.size());
		Logger.comment("skus_DB:"+skus_DB.size());
		Logger.comment("categorys_DB:"+categorys_DB.size());
		Logger.comment("brands_DB:"+brands_DB.size());
		
		//校验返回的数据
		Logger.verifyEquals("true", call.getSuccess(), "验证success返回true");
		//查询分期乐
		 Map<String, Object> mps=new HashMap<>();
		 mps.put("fenqileproduct", prodcuts_DB.get(0));
		 mps.put("fenqilesku", skus_DB.get(0));
		 mps.put("fenqilecategory", categorys_DB.get(0));
		 mps.put("fenqilebrand", brands_DB.get(0));
		 mps.put("searchFenqileSync", searchFenqileSync);
		 FQLOpenCallService.checkSKUMaps(mps);
	}
	//1：创建普通洋码头商品
	//2:检查数据是否同步到mongo
	//3:若同步到mongo,则添加到分期乐(前端查询是从mongo库中查询，然后添加到分期乐商品表)
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProduct_001() {
		Logger.start(true,"正例-添加商品进分期乐");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			//addpro.getProduct().setSecondCategoryId(768);
			addpro.getProduct().setThirdCategoryId(1003);
			addpro.getProduct().setBrandId(ProductManagerCallServiceV3.BrandId);
			addpro.getProduct().setProductName("型号");
			addpro.getProduct().getCatalogList().get(0).getPropertyList().get(0).setPropertyName("型号");
			
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			List<String> ids=new ArrayList();
			ids.add(productId);
			bean.setProductIdList(ids);
			call.setData(bean);
			//Thread.sleep(2000);
			//查询mongo
			int times=2000;
			do{
				Map<String,Object> p= mongoDB_productsWapper.getByProductId(productId);
				if((p==null ||p.size()==0 ))
				{
					Thread.sleep(times);
					p= mongoDB_productsWapper.getByProductId(productId);
					times+=2000;
				}else if(p.size()>0 )break;
			}while(times<=10000);
			
			if(times>10000)
				Logger.comment("商品未同步到mongo,无法添加分期乐商品");
			else 
				call.callService();
			Thread.sleep(2000);
		
			check_verify(productId,addpro);
			
//			//fenqilesku
//			skus_DB.forEach(b->{
//				System.out.println(b);
//			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProduct_002() {
		Logger.start(true,"反例-添加商品进分期乐-商品id中含有不存在的商品id");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			//addpro.getProduct().setSecondCategoryId(768);
			addpro.getProduct().setThirdCategoryId(1003);
			addpro.getProduct().setBrandId(ProductManagerCallServiceV3.BrandId);
			addpro.getProduct().setProductName("型号");
			addpro.getProduct().getCatalogList().get(0).getPropertyList().get(0).setPropertyName("型号");
			
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			List<String> ids=new ArrayList();
			ids.add(productId);
			ids.add("11111");
			bean.setProductIdList(ids);
			call.setData(bean);
			//Thread.sleep(2000);
			//查询mongo
			int times=2000;
			do{
				Map<String,Object> p= mongoDB_productsWapper.getByProductId(productId);
				if((p==null ||p.size()==0 ))
				{
					Thread.sleep(times);
					p= mongoDB_productsWapper.getByProductId(productId);
					times+=2000;
				}else if(p.size()>0 )break;
			}while(times<=10000);
			
			if(times>10000)
				Logger.comment("商品未同步到mongo,无法添加分期乐商品");
			else 
				call.callService();
			Thread.sleep(2000);
		
			//查询数据库fenqileproduct
			check_verify(productId,addpro);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProduct_003() {
		Logger.start(true,"反例-添加商品进分期乐-商品id为空");
		try {
			List<String> ids=new ArrayList();
			//ids.add("111");
			bean.setProductIdList(ids);
			call.setData(bean);	
			call.callService();
			Logger.verifyEquals("false", call.getSuccess(), "验证success返回true");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_addProduct_004() {
		Logger.start(true,"反例-添加商品进分期乐-商品id不存在");
		try {
			List<String> ids=new ArrayList();
			ids.add("111");
			bean.setProductIdList(ids);
			call.setData(bean);	
			call.callService();
			Logger.verifyEquals("false", call.getSuccess(), "验证success返回true");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}